package com.wang.util;

import com.wang.thread.LeakyBucketTimer;

import java.net.Socket;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 漏桶算法
 * Created by wang on 2017/5/3.
 */
public class LeakyBucket {
    //桶子
    private static final LinkedList<Socket> list = new LinkedList<>();

    //请求放入桶子
    public static boolean setRequest(Socket socket) {
        if (list.size() < 100) {
            list.addLast(socket);
            synchronized (list) {
                list.notify();
            }
            return true;
        } else {
            return false;
        }
    }
    //获取桶子容量
    public static int getBucketSize(){
        return list.size();
    }

    //取出请求
    public static Socket getSocket() {
        return list.pollFirst();

    }


    //开启漏桶线程
    public static void startLeakyTimer() {
        Thread thread = new Thread(new LeakyBucketTimer(list));
        thread.start();
    }

}
